//
//  GFTagsTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFTagsTableViewCell.h"

@interface GFTagsTableViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tagTableView;
//@property (nonatomic, strong) UILabel *otherLabel;
//@property (nonatomic, assign) BOOL infoSwitch;
@end

@implementation GFTagsTableViewCell
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatTableView];
//        [self creatLabel];

    }
    return self;
}
//#pragma mark - 创建label
//- (void)creatLabel {
//    self.otherLabel = [[UILabel alloc] init];
//    self.otherLabel.textColor = [UIColor whiteColor];
//    self.otherLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
//    self.otherLabel.font = [UIFont systemFontOfSize:12];
//}
#pragma mark - 创建tableView
- (void)creatTableView {
    self.tagTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    self.tagTableView.delegate = self;
    self.tagTableView.dataSource = self;
    self.tagTableView.bounces = NO;
    self.tagTableView.layer.masksToBounds = YES;
    self.tagTableView.layer.cornerRadius = 5;
    self.tagTableView.layer.borderWidth = 0.3;
    self.tagTableView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.contentView addSubview:self.tagTableView];
    [self.tagTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"other"];
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"other"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (0 == indexPath.row) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-604luxian"];
        
        if (self.model.place_geo_lat == 0 || self.model.place_geo_lng == 0) {
            cell.textLabel.text = @"暂无路线导航";
        } else {
            cell.textLabel.text = @"查询路线导航";
        }
    } else if (1 == indexPath.row) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-dianhua-2"];
        if (!(nil == self.model.phone) && !([self.model.phone isEqualToString:@""])) {
            cell.textLabel.text = @"拨打电话";
            
        } else {
            cell.textLabel.text = @"暂无电话";
        }
    } else if (2 == indexPath.row) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-xihuan-2"];
        
        NSArray *array = (NSArray *)self.model.food_list;
        if (!(0 == array.count)) {
            cell.textLabel.text = @"查看网友其他推荐";
        } else {
            cell.textLabel.text = @"暂无网友其他推荐";
        }

    } else {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-xiangqing-2"];
        cell.textLabel.text = @"餐馆其他信息";
    }
    return cell;
}
#pragma mark - row Number
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
#pragma mark - row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        return self.tagTableView.frame.size.height / 4;
    
}
#pragma mark - cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *vc = [self viewController];
    if (tableView == self.tagTableView) {
        if (0 == indexPath.row) {
            if (self.model.place_geo_lng == 0 || self.model.place_geo_lat == 0) {

            } else {
                GFLineViewController *lineVc = [[GFLineViewController alloc] initWithLat:self.model.place_geo_lat withLon:self.model.place_geo_lng withPlaceName:self.model.place_name withCity:self.model.city_name];
                [vc.navigationController pushViewController:lineVc animated:1];
            }

        } else if (1 == indexPath.row) {
            if (!(nil == self.model.phone) && !([self.model.phone isEqualToString:@""])) {
                NSMutableArray *array = [self phoneNumberSub];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"拨打电话" preferredStyle:UIAlertControllerStyleActionSheet];
                UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                for (int i = 0; i < array.count; i ++) {
                    UIAlertAction *action = [UIAlertAction actionWithTitle:array[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        [self phoneCallWithTel:array[i]];
                    }];
                    [alert addAction:action];
                }
                [alert addAction:actionCancel];
                UIViewController *vc = [self viewController];
                [vc presentViewController:alert animated:1 completion:nil];
                
            }
        } else if (2 == indexPath.row) {
            NSArray *array = (NSArray *)self.model.food_list;
            if (!(0 == array.count)) {
                UIViewController *vc = [self viewController];
                GFOtherFoodViewController *otherVc = [[GFOtherFoodViewController alloc] initWithId:self.model.myId withSort:array];
                [vc.navigationController pushViewController:otherVc animated:1];
            }
        } else {
            GFOtherTagsViewController *otVc = [[GFOtherTagsViewController alloc] initWithTags:self.model.otherTags withBus:self.model.bus];
            [vc.navigationController pushViewController:otVc animated:1];
//            self.infoSwitch = !self.infoSwitch;
//            if (self.infoSwitch) {
//                [self.contentView addSubview:self.otherLabel];
//            } else {
//                [self.otherLabel removeFromSuperview];
//            }
            
        }
    }

}
#pragma mark - 电话号拆分
- (NSMutableArray *)phoneNumberSub {
    NSInteger i = 0;
    if ([self.model.phone containsString:@";"]) {
        i = 1;
    } else if ([self.model.phone containsString:@" "]) {
        i = 2;
    }
    NSMutableArray *array = [NSMutableArray array];
    if (0 == i) {
        [array addObject:self.model.phone];
    } else if (1 == i) {
        NSArray *array1 = [self.model.phone componentsSeparatedByString:@";"];
                for (int i = 0; i < array1.count; i ++) {
                    [array addObject:array1[i]];
                }
    } else if (2 == i) {
        NSArray *array2 = [self.model.phone componentsSeparatedByString:@" "];
                for (int i = 0; i < array2.count; i ++) {
                    if (![array2[i] isEqualToString:@""]) {
                        [array addObject:array2[i]];
                    }
                }
    }
    return array;
//    NSArray *array1 = [self.model.phone componentsSeparatedByString:@";"];
//    NSArray *array2 = [self.model.phone componentsSeparatedByString:@" "];
//    NSLog(@"%@",array1);
//    NSLog(@"%@",array2);
    
//    if (array1.count != 0) {
//        for (int i = 0; i < array1.count; i ++) {
//            [array addObject:array1[i]];
//        }
//    }
//    if (array2.count != 0) {
//        for (int i = 0; i < array2.count; i ++) {
//            if (![array2[i] isEqualToString:@""]) {
//                [array addObject:array2[i]];
//            }
//        }
//    }

//    NSMutableArray *array = [NSMutableArray array];
//    NSInteger j = 0;
//    NSInteger k = 0;
//    for (NSInteger i = 0; i < self.model.phone.length; i ++) {
//        NSRange range = {static_cast<NSUInteger>(i),1};
//        NSString *str = [self.model.phone substringWithRange:range];
//        if ([str isEqualToString:@" "] || [str isEqualToString:@";"]) {
//            k = 0;
//            NSRange rangeSub = {static_cast<NSUInteger>(j),static_cast<NSUInteger>(i - j)};
//            NSString *string = [self.model.phone substringWithRange:rangeSub];
//            if ([string isEqualToString:@""] || [string isEqualToString:@";"]) {
//                
//            } else {
//               [array addObject:string];
//            }
//            
//            j = i + 1;
//        } else {
//            k = 1;
//
//        }
//    }

}
#pragma mark - 拨打电话
- (void)phoneCallWithTel:(NSString *)tel {
//    UIWebView *webView = nil;
//    if (webView == nil) {
//        webView = [[UIWebView alloc] init];
//    }
//    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", tel]];
//    NSLog(@"%@",url);
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    [webView loadRequest:request];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", tel]];
    
    [[UIApplication sharedApplication] openURL:url];
}
#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tagTableView.frame = CGRectMake(self.contentView.frame.size.width / 15, 0, ScreenWidth - self.contentView.frame.size.width / 15 * 2, self.contentView.frame.size.height / 1.2);
//    self.otherLabel.frame = CGRectMake(0, self.contentView.frame.size.height / 1.2, ScreenWidth, self.contentView.frame.size.height - self.contentView.frame.size.height / 1.2);
//    self.ViewForTags.backgroundColor = [UIColor redColor];
}
#pragma mark - set
- (void)setModel:(GFResturtantDetialModel *)model {
    if (_model != model) {
        _model = model;
    }
//    if (!(nil == self.model.otherTags) && !([self.model.otherTags isEqualToString:@""])) {
//        NSRange range = {self.model.otherTags.length - 1, 1};
//        self.otherLabel.text = [self.model.otherTags stringByReplacingCharactersInRange:range withString:@""];
//    } else {
//        self.otherLabel.text = @"暂无其他信息";
//    }
    [self.tagTableView reloadData];
}
- (void)awakeFromNib {
    // Initialization code
}
#pragma mark - 找到vc
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
